--
-- Author: yawei.zhu
-- Date: 2018-09-12 14:22:18
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
	if data then
		self:read(data)
	end
end

-- // 城镇信息
-- message TownInfo {
-- 	int32 id = 1;           // 城镇id
-- 	int32 prosperity = 2;   // 繁荣度
-- 	PHASE phase = 3; 		// 所处阶段
-- 	repeated Stronghold stronghold = 4; // 据点信息
-- 	int32 myBiddingPrice = 5; // 我的竞价（仅有权限的公会玩家才能查询，否则传0）
-- 	OwnerInfo owner = 6; // 占领者信息(为空表示当前没有占领者)
-- 	repeated int32 totalScore = 7; // 总比分（第一个值为防守方，第二个值为进攻方）如果值为0，可能会被procol优化为空值，客户端取用的时候注意判断长度
-- 	guildInfo def = 8; // 防守方信息(仅竞拍结束之后才会有数据，如果竞拍前就有领主，则这个值等于owner.info)
-- 	guildInfo atk = 9; // 进攻方信息(仅竞拍结束之后才会有数据)
-- 	int32 fightStartTime = 10; // 战斗开始时间(仅竞拍结束之后才会有数据)
-- 	int32 fightEndTime = 11; // 战斗结束时间(仅竞拍结束之后才会有数据)
-- }

-- message Stronghold {
-- 	int32 id = 1; // 据点id
-- 	int32 townId = 2; // 从属城镇id
-- 	repeated int32 realNum = 3; // 实时人数（第一个值为防守方，第二个值为进攻方）如果值为0，可能会被procol优化为空值，客户端取用的时候注意判断长度
-- 	repeated int32 realScore = 4; // 实时比分（第一个值为防守方，第二个值为进攻方）如果值为0，可能会被procol优化为空值，客户端取用的时候注意判断长度
-- }

-- message guildInfo {
-- 	string guildName = 1; 		         // 公会名称
-- 	string guildId = 2;					 // 公会id
-- 	string flagBaseIcon = 3;             // 战旗基础图案
-- 	string flagAdditionalIcon = 4;       // 战旗附加图案
-- 	int32  flagBaseIconColor = 5;         // 战旗基础图案颜色
-- 	int32  flagAdditionalIconColor = 6;   // 战旗附加图案颜色
-- }

-- // 城镇占领者信息
-- message OwnerInfo {
-- 	int32  date = 1; // 占领时间（2180101）
-- 	guildInfo info = 2; // 这个功能模块需要用到的部分公会信息
-- }

-- 工会信息
function ClassRef:createGuildInfo(guildInfo)
	if not guildInfo then
		guildInfo = {}
	end
	local info = {}
	info.guildName = guildInfo.guildName or ""
	info.guildId = guildInfo.guildId or 0
	info.flagBaseIcon = guildInfo.flagBaseIcon or ""
	info.flagAdditionalIcon = guildInfo.flagAdditionalIcon or ""
	info.flagBaseIconColor = guildInfo.flagBaseIconColor or 0
	info.flagAdditionalIconColor = guildInfo.flagAdditionalIconColor or 0
	return info
end

function ClassRef:read(townInfo)
	self.id = townInfo.id
	self.prosperity = townInfo.prosperity
	-- self.phase = protobuf.enum_id("pb_town.PHASE", townInfo.phase) or 0
	self.phase = townInfo.phase
	self.myBiddingPrice = townInfo.myBiddingPrice

	self.totalScoreDef = townInfo.totalScore[1] or 0
	self.totalScoreAtk = townInfo.totalScore[2] or 0

	self.defScorePerSecond = townInfo.defScorePerSecond or 0
	self.atkScorePerSecond = townInfo.atkScorePerSecond or 0

	self.fightStartTime = townInfo.fightStartTime or 0
	self.fightEndTime = townInfo.fightEndTime or 0

	-- 城镇占领者
	self.owner = nil
	if townInfo.owner then
		local data = {}
		data.date = townInfo.owner.date
		data.guildInfo = self:createGuildInfo(townInfo.owner.info)
		self.owner = data
	end
	-- 防守方
	self.defer = self:createGuildInfo(townInfo.def)
	-- 进攻方
	self.atker = self:createGuildInfo(townInfo.atk)

	-- 据点信息
	self.strongholds = {}
	for _, v in ipairs(townInfo.stronghold) do
		local data = {}
		data.id = v.id
		data.townId = v.townId
		data.realNumDef = v.realNum[1] or 0
		data.realNumAtk = v.realNum[2] or 0
		data.realScoreDef = v.realScore[1] or 0
		data.realScoreAtk = v.realScore[2] or 0
		data.realSpeedDef = v.defScoreSpeed or 0 
		data.realSpeedAtk = v.atkScoreSpeed or 0
		
		table.insert(self.strongholds, data)
	end

	-- 根据据点ID进行排序
	table.sort(self.strongholds, function(a, b)
		return a.id < b.id
	end)

	-- dump(self)
end

function ClassRef:test()
	local townInfo = {}
	townInfo.id = 1
	townInfo.prosperity = 100
	townInfo.phase = "FIGHT"
	townInfo.myBiddingPrice = 1000

	townInfo.totalScore = {100, 200}
	townInfo.fightStartTime = 1536811200
	townInfo.fightEndTime = 1537272000

	townInfo.stronghold = {}
	for i = 1, 5 do
		local data = {}
		data.id = i
		data.townId = 1
		data.realNum = {10, 20}
		data.realScore = {11, 21}
		data.defScoreSpeed = 10
		data.atkScoreSpeed = 20

		if i == 3 then 
			data.realNum = {20, 10}
			data.realScore = {21, 11}
		end
		table.insert(townInfo.stronghold, data)
	end

	local data = {}
	data.guildName = "111111111"
	data.guildId = 1
	data.flagBaseIcon = "Gicon_u_04"
	data.flagAdditionalIcon = "Gicon_u_04"
	data.flagBaseIconColor = 8
	data.flagAdditionalIconColor = 8
	townInfo.def = data

	data = {}
	data.guildName = "22222222"
	data.guildId = 2
	data.flagBaseIcon = "Gicon_u_03"
	data.flagAdditionalIcon = "Gicon_u_03"
	data.flagBaseIconColor = 8
	data.flagAdditionalIconColor = 8
	townInfo.atk = data

	self:read(townInfo)
end

function ClassRef:checkCanEnterByGuildId(guildId)
	-- dump(guildId)
	if not guildId or guildId == 0 then return false end
	if self.defer.guildId == guildId then return true end
	if self.atker.guildId == guildId then return true end
	return false
end

function ClassRef:getIndexByStrongholdId(strongholdId)
	if not strongholdId then return 1 end
	for index, holdInfo in ipairs(self.strongholds) do
		if holdInfo.id == strongholdId then
			return index
		end
	end
	return 1
end

return ClassRef
